package com.fastloan.jn.mgt.customermanager.basic.contorller;

import com.fastloan.jn.mgt.application.basic.model.vo.AppCustomerExt;
import com.fastloan.jn.mgt.application.basic.model.vo.AppExt;
import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.fastloan.jn.mgt.customermanager.basic.model.JnCustomerManager;
import com.fastloan.jn.mgt.customermanager.basic.model.vo.JnCustomerManagerTree;
import com.fastloan.jn.mgt.customermanager.basic.model.vo.ManagerPerformanceExt;
import com.fastloan.jn.mgt.customermanager.basic.service.JnCustomerManagerService;
import com.fastloan.jn.mgt.customermanager.daily.service.JnCustomerManagerDailyService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户经理Controller
 * @author yanwe
 * createTime 2017-10-2017/10/17 19:41
 */
@RestController
@RequestMapping("/api/web/customerManager")
public class JnCustomerManagerController extends BaseController<JnCustomerManager> {

    @Autowired
    private JnCustomerManagerService jnCustomerManagerService;

    @Autowired
    private JnApplicationService applicationService;

    @Autowired
    private JnCustomerManagerDailyService dailyService;

    @Autowired
    private CmuUserService userService;


    @PostMapping("/pageList")
    public ResultTo selectPage(){
        return dataPage("selectCusMgrTreeByUserId");
    }

    @GetMapping("/org")
    public ResultTo selectOrgCusMgr(@RequestParam(value = "orgId",required = false) Integer orgId) {
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId == null? 1 : orgId);
        return new ResultTo().setData(jnCustomerManagerService.selectCusMgrTreeByOrgId(map));
    }

    @GetMapping("/{userId}")
    public ResultTo selectByUserId(@PathVariable("userId") String userId){
        return new ResultTo().setData(jnCustomerManagerService.selectByUserId(userId));
    }

    @GetMapping("/tree")
    public ResultTo selectTree(Integer userId){
        List<JnCustomerManagerTree> jnCustomerManagerTrees;
        if (userId == null){
            jnCustomerManagerTrees = jnCustomerManagerService.selectTierTreeByOrgId(1);
        } else {
            jnCustomerManagerTrees = jnCustomerManagerService.selectTierTreeByUserId(userId);
        }
        return new ResultTo().setData(jnCustomerManagerTrees);
    }

    @PostMapping
    public ResultTo addCustomerManager(JnCustomerManager jnCustomerManager){
        boolean result = jnCustomerManagerService.createCustomerManager(jnCustomerManager);
        return result ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    @PutMapping
    public ResultTo updateCustomerManager(JnCustomerManager jnCustomerManager) {
        boolean result = jnCustomerManagerService.updateCustomerManager(jnCustomerManager);
        return result ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }



    @PostMapping("/customers")
    public ResultTo customers(String id){
        List<String> ids;
        if(id == null){
            String managerId = ManagerKit.getUserId();
            ids = jnCustomerManagerService.managerIds(managerId);
        }else{
            ids = new ArrayList<>();
            ids.add(id);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        return dataPage(applicationService,AppCustomerExt.class,"appCustomerExt",map);
    }

    @PostMapping("/myOrgManagers")
    public ResultTo myOrgManagers(){
        List<String> ids = dailyService.topOrgManagerIds(ManagerKit.getTopOrgId());
        Example example = new Example(CmuUser.class);
        example.createCriteria().andIn("id",ids);
        return new ResultTo().setData(userService.selectByExample(example));
    }


    @PostMapping("/orgManagers")
    public ResultTo orgManagers(String orgId){
        Map<String,Object> map = new HashMap<>();
        map.put("orgId",orgId);
        return dataPage(jnCustomerManagerService,CmuUser.class,"queryOrgManagers",map);
    }

    @PostMapping("/performance")
    public ResultTo performance(String username){
        List<String> ids = dailyService.topOrgManagerIds(ManagerKit.getTopOrgId());
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("ids",ids);
        return dataPage(jnCustomerManagerService,ManagerPerformanceExt.class,"queryManagerPerformance",map);
    }

    @PostMapping("/managerApp")
    public ResultTo managerApp(String userId){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        return dataPage(applicationService,AppExt.class,"selectAllAppExt",map);
    }


}
